package com.jt.service;

import com.jt.base.BaseService;
import com.jt.domain.dao.SidebarNode;
import com.jt.domain.mapper.SidebarNodeMapper;
import com.jt.entity.SideBar;
import com.jt.util.JsonTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by kiktech on 2017/4/25.
 */
@Service
public class SidebarNodeService extends BaseService<SidebarNode> {

    @Autowired
    private SidebarNodeMapper sidebarNodeMapper;

    @Autowired
    private JsonTemplate jsonTemplate;

    public SideBar getSideBar() {
        SideBar sideBar = new SideBar();
        List<SidebarNode> sidebarNodeList = sidebarNodeMapper.selectAll();
        if (null != sidebarNodeList && sidebarNodeList.size() > 0) {
            List<SideBar.Node> nodeList = new ArrayList<>();
            sidebarNodeList.forEach((s) -> {
                SideBar.Node node = new SideBar.Node();
                BeanUtils.copyProperties(s, node);
                node.setId(s.getSid());
                nodeList.add(node);
            });
            Map<String, List<SideBar.Node>> groupbyParentSidMap = nodeList
                    .stream()
                    .collect(Collectors.groupingBy(SideBar.Node::getParentSid));
            List<SideBar.Node> pList = groupbyParentSidMap.get("");
            if (null != pList && pList.size() > 0) {
                pList.forEach((s) -> {
                    String id = s.getId();
                    s.setNodes(groupbyParentSidMap.get(id));
                });
            }
        }
        return sideBar;
    }

}
